using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace Lizk.SimpleHUD
{
    [Serializable]
    public class HUDLabel : HUDControl
    {
        private string text = "";
        private ColorS color = ColorS.White;
        private string font = "";

        public RectangleS SourceRectangle { get; set; }

#if !XBOX
        [Browsable(false)]
#endif        
        private bool dirty = true;
        private List<HUDFont.DrawInfo> drawInfos = new List<HUDFont.DrawInfo>();

        public string Font { get { return font; } set { font = value; dirty = true; } }

        public bool Dirty
        {
            get { return dirty; }
            set { dirty = value; }
        }

        private Alignment alignment = Alignment.Top | Alignment.Left;

        public ColorS Color
        {
            get { return color; }
            set
            {
                if (color != value)
                    dirty = true;
                color = value;
                
            }
        }

        public SizeS GetFittedSize(SizeS maxSize)
        {
            List<HUDFont.DrawInfo> infos = HUDFont.GetFont(font).TextBox2(new RectangleS(characterOffset, maxSize), Color, this.Alignment, wordBreak, this.text);
            float right = 0, bottom = 0;
            foreach (var g in infos)
            {
                right = Math.Max(g.Destination.Right, right);
                bottom = Math.Max(g.Destination.Bottom, bottom);
            }
            return new SizeS(right, bottom);
        }


        public List<HUDFont.DrawInfo> GetCharInfo()
        {
            if (dirty)
            {
                drawInfos.Clear();
                drawInfos = HUDFont.GetFont(font).TextBox2(new RectangleS(characterOffset, Size), Color, this.Alignment, wordBreak, this.text);
                dirty = false;
            }
            return drawInfos;
        }

        private PointS characterOffset = PointS.Empty;

        public PointS CharacterOffset
        {
            get { return characterOffset; }
            set { characterOffset = value; }
        }


        public string Text
        {
            get { return text; }
            set
            {
                if (text == value)
                    return;
                text = value;
                dirty = true;
            }
        }

        private bool wordBreak = true;

        public bool WordBreak
        {
            get { return wordBreak; }
            set
            {
                wordBreak = value;
                dirty = true;
            }
        }
#if !XBOX
//        [TypeConverter(typeof(FlagsEnumConverter))]
#endif
        public Alignment Alignment { get { return alignment; } set
        {
            alignment = value;
            dirty = true;
        }
        }

        public override void ResizeMove(RectangleS oldRect, RectangleS newRect)
        {
            if (oldRect.Size != newRect.Size)
            {
                dirty = true;
            }
            base.ResizeMove(oldRect, newRect);
        }

        public HUDLabel() { }

        public HUDLabel(string text, string font)
        {
            this.font = font;
            Text = text;
        }
    }
}